﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WebSite.admin.product
{
    public partial class prosoryadd : System.Web.UI.Page
    {
        // Fields

      //  private Image.GetThumbnailImageAbort callb;

        protected string imageName;
        protected string imagePath;
        protected string imageType;
        private System.Drawing.Image newimage;
        public string spe = "0";
        public string spestr = "";
        System.Drawing.Image image;
        // Methods
        private void AddPicture(string str, string strprice, string proid)
        {
            SQLAccess.DoSql("insert into DB_Spe(proid,spename,speprice) values(" + proid.ToString() + ",'" + str.ToString() + "'," + strprice.ToString() + ")");
        }

        private void Bind()
        {
            if (base.Request.QueryString["pid"] != null)
            {
                DataSet set = SQLAccess.GreatDs("select DB_Memberank.rankid,DB_Memberank.rank,DB_Memberprice.productid,DB_Memberprice.price from DB_memberank,DB_Memberprice where DB_Memberprice.rankid=DB_memberank.rankid and DB_memberprice.productid=" + base.Request.QueryString["pid"] + " order by DB_Memberank.rankid asc");
                this.DataList1.DataSource = set;
                this.DataList1.DataBind();
            }
            else
            {
                string str = "select * from DB_memberank order by rankid asc";
                DataSet set2 = SQLAccess.GreatDs(str);
                this.DataList1.DataSource = set2;
                this.DataList1.DataBind();
            }
            string sql = "select * from DB_Pinpai where visible=1 and languages=" + info.languages("") + " order by pinpaiorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.propinpai.Items.Add(new ListItem(row["pinpainame"].ToString(), row["pinpaiid"].ToString()));
                }
            }
            this.prodate1.Text = DateTime.Today.ToShortDateString();
            this.prodate2.Text = DateTime.Today.ToShortDateString();
            if (((base.Request.QueryString["sid"] != null) && (base.Request.QueryString["action"] != null)) && (base.Request.QueryString["pid"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("delete from DB_Spe where speid=" + base.Request.QueryString["sid"].ToString() + "");
                }
                base.Response.Write("<script language='javascript'>window.location.href='prosoryadd.aspx?pid=" + base.Request.QueryString["pid"] + "';</script>");
                base.Response.End();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            upload upload = new upload();
            string str2 = upload.Up(this.file1, "../../uploadfile/product/");
            base.Response.Write(upload.Resup[Convert.ToInt32(str2)]);
            this.bigpic.Text = upload.s;
            if (this.CheckBox1.Checked)
            {
                this.imageName = upload.s;
                string str = base.Server.MapPath("../../uploadfile/product");
                this.image = System.Drawing. Image.FromFile(str + @"\" + this.imageName);
                if ((Convert.ToInt32(info.info1("", "shopwidth")) > 0) && (Convert.ToInt32(info.info1("", "shopheight")) > 0))
                {
                    MakeThumbnail(str + @"\" + this.imageName, str + @"\small" + this.imageName, Convert.ToInt32(info.info1("", "shopwidth")), Convert.ToInt32(info.info1("", "shopheight")), "HW");
                }
                else if ((Convert.ToInt32(info.info1("", "shopwidth")) > 0) && (Convert.ToInt32(info.info1("", "shopheight")) == 0))
                {
                    MakeThumbnail(str + @"\" + this.imageName, str + @"\small" + this.imageName, Convert.ToInt32(info.info1("", "shopwidth")), 120, "W");
                }
                else if ((Convert.ToInt32(info.info1("", "shopwidth")) == 0) && (Convert.ToInt32(info.info1("", "shopheight")) > 0))
                {
                    MakeThumbnail(str + @"\" + this.imageName, str + @"\small" + this.imageName, 120, Convert.ToInt32(info.info1("", "shopheight")), "H");
                }
                else
                {
                    MakeThumbnail(str + @"\" + this.imageName, str + @"\small" + this.imageName, 120, 120, "HW");
                }
                this.smallpic.Text = "small" + this.imageName;
                this.image.Dispose();
            }
            if (base.Request.QueryString["pid"] != null)
            {
                this.upbind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            upload upload = new upload();
            string str = upload.Up(this.file2, "../../uploadfile/product/");
            base.Response.Write(upload.Resup[Convert.ToInt32(str)]);
            this.smallpic.Text = upload.s;
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Productclass Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
                    this.Display(row["classid"].ToString(), "|-" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string str3;
            string str4;
            long num;
            if (this.typeid.SelectedItem.Value.ToString() != "0")
            {
                str3 = this.typeid.SelectedItem.Value.ToString();
            }
            else
            {
                str3 = "0";
            }
            string sql = "select * from DB_Shoppro order by proid desc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                num = Convert.ToInt32(table.Rows[0]["proid"]) + 1;
            }
            else
            {
                num = 1L;
            }
            if (re.replace(this.procode.Value) == "")
            {
                if (num < 10L)
                {
                    str4 = info.info1("", "shophou") + "0000" + num.ToString();
                }
                else if ((num < 100L) && (num >= 10L))
                {
                    str4 = info.info1("", "shophou") + "000" + num.ToString();
                }
                else if ((num < 0x3e8L) && (num >= 100L))
                {
                    str4 = info.info1("", "shophou") + "00" + num.ToString();
                }
                else if ((num < 0x2710L) && (num >= 0x3e8L))
                {
                    str4 = info.info1("", "shophou") + "0" + num.ToString();
                }
                else
                {
                    str4 = info.info1("", "shophou") + num.ToString();
                }
            }
            else
            {
                str4 = re.replace(this.procode.Value);
            }
            string[] strArray = base.Request.Form["text"].Split(new char[] { ',' });
            string[] strArray2 = base.Request.Form["textprice"].Split(new char[] { ',' });
            sql = "select * from DB_memberank order by rankid asc";
            DataTable table2 = new DataTable();
            table2 = SQLAccess.GreatDs(sql).Tables[0];
            if (table2.Rows.Count > 0)
            {
                foreach (DataRow row in table2.Rows)
                {
                    string str5;
                    if (re.replace(base.Request.Form["proprice2_" + row["rankid"].ToString()]) == "-1")
                    {
                        str5 = ((Convert.ToSingle(re.replace(this.proprice1.Value)) * Convert.ToSingle(row["score3"].ToString())) / 100f) + "";
                    }
                    else
                    {
                        str5 = Convert.ToSingle(re.replace(base.Request.Form["proprice2_" + row["rankid"].ToString()])) + "";
                    }
                    if (base.Request.QueryString["pid"] != null)
                    {
                        SQLAccess.DoSql("update DB_Memberprice set [price]=" + str5 + " where productid=" + re.replace(base.Request.QueryString["pid"]) + " and rankid=" + row["rankid"].ToString() + "");
                    }
                    else
                    {
                        SQLAccess.DoSql("insert into [DB_Memberprice] (rankid,productid,[price]) values (" + row["rankid"].ToString() + "," + num.ToString() + "," + str5 + ")");
                    }
                }
            }
            if (base.Request.QueryString["pid"] != null)
            {
                this.upcontent();
                string str6 = "select * from DB_Spe where proid=" + base.Request.QueryString["pid"] + "";
                DataTable table3 = new DataTable();
                table3 = SQLAccess.GreatDs(str6).Tables[0];
                if (table3.Rows.Count > 0)
                {
                    this.spe = "1";
                }
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i].ToString() != "")
                    {
                        this.AddPicture(strArray[i], strArray2[i], base.Request.QueryString["pid"]);
                        this.spe = "1";
                    }
                }
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Shoppro set typeid=", str3, ",proname='", re.replace(this.proname.Value), "',procode='", re.replace(this.procode.Value), "',propinpai=", this.propinpai.SelectedValue, ",proprice1=", re.replace(this.proprice1.Value), ",proprice5=", re.replace(this.proprice5.Value), ",proprice3=", re.replace(this.proprice3.Value), ",proscore1=", re.replace(this.proscore1.Value), 
                ",proscore2=", re.replace(this.proscore2.Value), ",proprice4=", re.replace(this.proprice4.Text), ",promsg='", re.replace(this.promsg.Text), "',iscuxao=", this.iscuxao.Checked  ? "1" : "0", ",prodate1='", re.replace(this.prodate1.Text), "',prodate2='", re.replace(this.prodate2.Text), "',propic1='", re.replace(this.bigpic.Text), "',propic2='", re.replace(this.smallpic.Text), 
                "',spe=", this.spe.ToString(), ",protype=1,procontent='", this.WE_NewsContent.Text.Replace("'", "''"), "' where proid=", base.Request.QueryString["pid"], ""
             }));
                base.Response.Write("<script language='javascript'>alert('配件信息修改成功');window.location.href='prosory.aspx';</script>");
                base.Response.End();
            }
            else
            {
                for (int j = 0; j < strArray.Length; j++)
                {
                    if (strArray[j].ToString() != "")
                    {
                        this.AddPicture(strArray[j], strArray2[j], num.ToString());
                        this.spe = "1";
                    }
                }
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_Shoppro ([proid],proname,procode,typeid,propinpai,proprice1,proprice3,proscore1,proscore2,proprice4,iscuxao,prodate1,prodate2,propic1,propic2,proku1,proku2,proisshow,proisp,visible,updatetime,languages,proprice5,promsg,spe,procontent,protype) values (", num.ToString(), ",'", re.replace(this.proname.Value), "','", re.replace(str4.ToString()), "',", str3, ",", re.replace(this.propinpai.SelectedValue), ",", re.replace(this.proprice1.Value), ",", re.replace(this.proprice3.Value), ",", re.replace(this.proscore1.Value), 
                ",", re.replace(this.proscore2.Value), ",", re.replace(this.proprice4.Text), ",", this.iscuxao.Checked ? "1" : "0", ",'", re.replace(this.prodate1.Text), "','", re.replace(this.prodate2.Text), "','", re.replace(this.bigpic.Text), "','", re.replace(this.smallpic.Text), "',", re.replace(info.info1("", "shopmoku")), 
                ",", re.replace(info.info1("", "shopmoku")), ",1,1,1,'", DateTime.Now, "',", info.languages(""), ",", re.replace(this.proprice5.Value), ",'", re.replace(this.promsg.Text), "',", this.spe.ToString(), ",'", this.WE_NewsContent.Text.Replace("'", "''"), "',1)"
             }));
                base.Response.Write("<script language='javascript'>alert('配件信息添加成功');window.location.href='prosory.aspx';</script>");
                base.Response.End();
            }
        }

        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        {
            string str;
           System.Drawing.Image image = System.Drawing.Image.FromFile(originalImagePath);
            int num = width;
            int num2 = height;
            int x = 0;
            int y = 0;
            int num5 = image.Width;
            int num6 = image.Height;
            if (((str = mode) != null) && (str != "HW"))
            {
                if (!(str == "W"))
                {
                    if (str == "H")
                    {
                        num = (image.Width * height) / image.Height;
                    }
                    else if (str == "Cut")
                    {
                        if ((((double)image.Width) / ((double)image.Height)) > (((double)num) / ((double)num2)))
                        {
                            num6 = image.Height;
                            num5 = (image.Height * num) / num2;
                            y = 0;
                            x = (image.Width - num5) / 2;
                        }
                        else
                        {
                            num5 = image.Width;
                            num6 = (image.Width * height) / num;
                            x = 0;
                            y = (image.Height - num6) / 2;
                        }
                    }
                }
                else
                {
                    num2 = (image.Height * width) / image.Width;
                }
            }
           System.Drawing. Image image2 = new Bitmap(num, num2);
            Graphics graphics = Graphics.FromImage(image2);
            graphics.InterpolationMode = InterpolationMode.High;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.Transparent);
            graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
            try
            {
                image2.Save(thumbnailPath, ImageFormat.Jpeg);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton1.Attributes["onclick"] = "return CheckFrm();";
            this.iscuxao.Attributes["onclick"] = "handlePromote(iscuxao);";
            this.CheckBox1.Attributes["onclick"] = "handleAutoThumb(CheckBox1);";
            if (!base.IsPostBack)
            {
                this.Bind();
                this.Display("268", "|--");
                if (base.Request.QueryString["pid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select top 1 * from DB_Shoppro where proid=" + base.Request.QueryString["pid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.proname.Value = table.Rows[0]["proname"].ToString();
            this.procode.Value = table.Rows[0]["procode"].ToString();
            this.typeid.SelectedValue = table.Rows[0]["typeid"].ToString();
            this.propinpai.SelectedValue = table.Rows[0]["propinpai"].ToString();
            this.proprice1.Value = table.Rows[0]["proprice1"].ToString();
            this.proprice3.Value = table.Rows[0]["proprice3"].ToString();
            this.proprice5.Value = table.Rows[0]["proprice5"].ToString();
            this.proscore1.Value = table.Rows[0]["proscore1"].ToString();
            this.proscore2.Value = table.Rows[0]["proscore2"].ToString();
            this.iscuxao.Checked = (bool)table.Rows[0]["iscuxao"];
            this.prodate1.Text = table.Rows[0]["prodate1"].ToString();
            this.prodate2.Text = table.Rows[0]["prodate2"].ToString();
            this.bigpic.Text = table.Rows[0]["propic1"].ToString();
            this.smallpic.Text = table.Rows[0]["propic2"].ToString();
            this.promsg.Text = table.Rows[0]["promsg"].ToString();
            this.WE_NewsContent.Text = table.Rows[0]["procontent"].ToString();
            string str2 = "select * from DB_Spe where proid=" + base.Request.QueryString["pid"] + " order by speid asc";
            DataTable table2 = new DataTable();
            table2 = SQLAccess.GreatDs(str2).Tables[0];
            if (table2.Rows.Count > 0)
            {
                foreach (DataRow row in table2.Rows)
                {
                    string spestr = this.spestr;
                    this.spestr = spestr + "<a href='prosoryadd.aspx?sid=" + row["speid"].ToString() + "&pid=" + base.Request.QueryString["pid"] + "&action=del'>[-]</a>";
                    string str4 = this.spestr;
                    this.spestr = str4 + "规格名：<input name='content_" + row["speid"].ToString() + "' value='" + row["spename"].ToString() + "' type='text' style='width: 50px' />价格：<input name='price_" + row["speid"].ToString() + "' value='" + row["speprice"].ToString() + "' type='text'  style='width: 50px' /><br />";
                }
                this.spe = table.Rows[0]["spe"].ToString();
            }
        }

        private void upcontent()
        {
            string sql = "select * from DB_Spe where proid=" + base.Request.QueryString["pid"] + " order by speid asc";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    string str2;
                    if ((base.Request.Form["price_" + row["speid"].ToString()] == null) || (base.Request.Form["price_" + row["speid"].ToString()] == null))
                    {
                        str2 = "0";
                    }
                    else
                    {
                        str2 = base.Request.Form["price_" + row["speid"].ToString()];
                    }
                    SQLAccess.DoSql("update [DB_Spe] set [spename]='" + base.Request.Form["content_" + row["speid"].ToString()] + "',speprice=" + str2 + " where speid=" + row["speid"].ToString() + " and proid=" + base.Request.QueryString["pid"] + "");
                }
            }
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}